﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.goods
{
    public partial class brandManager : BaseCrm
    {
        readonly t_DALBrand theBrand = new t_DALBrand();
        t_Brand oBrand;

        protected void Page_Load(object sender, EventArgs e)
        {


            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(eRule.BrandManager);
                btnBrachDeleted.OnClientClick = Grid1.GetNoSelectionAlertInTopReference("没有选中项！");
                this.DeleletBrand();
                this.BindGrid();

            }
        }
        private void BindGrid()
        {

            DataTable table = theBrand.GetListNoDeleted(string.Format(" brandName like '%{0}%'", this.txtKeywords.Text.Trim())).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();

        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;

        }

        void DeleletBrand()
        {
            if (Request.QueryString["action"] == "del")
            {
                theBrand.Delete(int.Parse(Request.QueryString["bid"]));
                ExtAspNet.Alert.Show("删除品牌成功！");
            }
        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theBrand.GetListNoDeleted("").Tables[0];
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

        protected void btnBrachDeleted_Click1(object sender, EventArgs e)
        {

            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sbBrands = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
                if (i == selectedCount - 1)
                {
                    sbBrands.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
                }
                else
                {
                    sbBrands.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
                }
            }
            if (sbBrands.Length > 0)
            {
                theBrand.DeleteList(sbBrands.ToString());
            }
            ExtAspNet.Alert.Show("批量删除品牌成功！");
            this.BindGrid();
        }

    }
}